package com.jyk.blog.service.impl;

import com.jyk.blog.mapper.BlogTagMapper;
import com.jyk.blog.po.Blog;
import com.jyk.blog.po.Tag;
import com.jyk.blog.service.BlogService;
import com.jyk.blog.service.BlogTagService;
import com.jyk.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BlogTagServiceImpl implements BlogTagService {
    @Autowired
    BlogTagMapper blogTagMapper;
    @Autowired
    BlogService blogService;
    @Autowired
    TagService tagService;
    @Override
    public List<Blog> queryByTag(int id) {
        List<Integer> idList = blogTagMapper.findByTag(id);
        List<Blog> blogs = new ArrayList<>();
        for (Integer integer : idList) {
            blogs.add(blogService.queryById(integer));
        }
        return blogs;
    }

    @Override
    public List<Tag> queryByBlog(int id) {
        List<Integer> idList = blogTagMapper.findByBlog(id);
        List<Tag> tags = new ArrayList<>();
        for (Integer integer : idList) {
            tags.add(tagService.queryById(integer));
        }
        return tags;
    }

    @Override
    public void insertOne(int blogId, int tagId) {
        blogTagMapper.insert(blogId, tagId);
    }

    @Override
    public void deleteByBlog(int blogID) {
        blogTagMapper.deleteByBlog(blogID);
    }

    @Override
    public void deleteByTag(int tagID) {
        blogTagMapper.deleteByTag(tagID);
    }
}
